﻿using Caliburn.Micro;
using OPSYS_Schematic.UIInfrastructure;

namespace OPSYS_Schematic.Presentation.Parts
{
    //[MenuPart(PreviousMenu = WorkbenchName.EditPart, NextMenu = WorkbenchName.ToolPart)]
    [MenuPart(BaseMenu = WorkbenchName.GeneralPart, PreviousMenu = WorkbenchName.EditPart)]
    public class ViewPart : MenuPart
    {
        private CollectionMerger<IMenuPart> merger;
        private CollectionSyncer<IDockScreen, IMenuPart> syncer;

        public ViewPart()
            : base(WorkbenchName.ViewPart)
        {
        }

        public override void OnAttached()
        {
            syncer = new CollectionSyncer<IDockScreen, IMenuPart>(IoC.Get<IDockScreenManager>().Screens, (item) =>
                                                                                                             {
                                                                                                                 return
                                                                                                                     new MenuPart
                                                                                                                         (item
                                                                                                                              .
                                                                                                                              Name,
                                                                                                                          (
                                                                                                                              i)
                                                                                                                          =>
                                                                                                                              {
                                                                                                                                  var
                                                                                                                                      menu
                                                                                                                                          =
                                                                                                                                          i
                                                                                                                                          as
                                                                                                                                          MenuPart;
                                                                                                                                  menu
                                                                                                                                      .
                                                                                                                                      IsChecked
                                                                                                                                      =
                                                                                                                                      !menu
                                                                                                                                           .
                                                                                                                                           IsChecked;
                                                                                                                                  if
                                                                                                                                      (
                                                                                                                                      menu
                                                                                                                                          .
                                                                                                                                          IsChecked)
                                                                                                                                  {
                                                                                                                                      IoC
                                                                                                                                          .
                                                                                                                                          Get
                                                                                                                                          <
                                                                                                                                              IDockScreenManager
                                                                                                                                              >
                                                                                                                                          ()
                                                                                                                                          [
                                                                                                                                              menu
                                                                                                                                                  .
                                                                                                                                                  Name
                                                                                                                                          ]
                                                                                                                                          .
                                                                                                                                          Show
                                                                                                                                          ();
                                                                                                                                  }
                                                                                                                                  else
                                                                                                                                  {
                                                                                                                                      IoC
                                                                                                                                          .
                                                                                                                                          Get
                                                                                                                                          <
                                                                                                                                              IDockScreenManager
                                                                                                                                              >
                                                                                                                                          ()
                                                                                                                                          [
                                                                                                                                              menu
                                                                                                                                                  .
                                                                                                                                                  Name
                                                                                                                                          ]
                                                                                                                                          .
                                                                                                                                          Close
                                                                                                                                          ();
                                                                                                                                  }
                                                                                                                              })
                                                                                                                         {
                                                                                                                             DisplayName
                                                                                                                                 =
                                                                                                                                 item
                                                                                                                                 .
                                                                                                                                 DisplayName,
                                                                                                                             IsCheckable
                                                                                                                                 =
                                                                                                                                 true,
                                                                                                                             IsChecked
                                                                                                                                 =
                                                                                                                                 true
                                                                                                                         };
                                                                                                             });

            merger = new CollectionMerger<IMenuPart>((this as IObservableParent<IMenuPart>).Items, syncer.SyncCollection);
        }
    }
}